using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class DAO_Question_Figure
    {
        public static bool Insert(Object tmp)
        {
            QUESTION_FIGURE obj = (QUESTION_FIGURE)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));
            _list.Add(new SqlParameter("@FIGURE_ID", obj.FIGURE_ID));
            _list.Add(new SqlParameter("@QUESTION_FIGURE_DESC", obj.QUESTION_FIGURE_DESC));            

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_figure_insert", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static QUESTION_FIGURE Select(int id)
        {
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@QUESTION_ID", id));

            DataTable table = clsDataAccessHelper.ExecuteQuery("sp_selete_question_figure", _list);
            if (table != null && table.Rows.Count == 1)
            {
                QUESTION_FIGURE question = new QUESTION_FIGURE();
                question.FIGURE_ID = decimal.Parse(table.Rows[0]["FIGURE_ID"].ToString());
                question.QUESTION_FIGURE_DESC = table.Rows[0]["QUESTION_FIGURE_DESC"].ToString();
                question.QUESTION_FIGURE_ID = decimal.Parse(table.Rows[0]["FIGURE_ID"].ToString());
                question.QUESTION_ID = id;
                return question;
            }
            return null;
        }

        public static bool Update(Object tmp)
        {
            QUESTION_FIGURE obj = (QUESTION_FIGURE)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@FIGURE_ID", obj.FIGURE_ID));
            _list.Add(new SqlParameter("@QUESTION_FIGURE_DESC", obj.QUESTION_FIGURE_DESC));
            _list.Add(new SqlParameter("@QUESTION_FIGURE_ID", obj.QUESTION_FIGURE_ID));
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_figure_update", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static string CurrentInsertID()
        {
            List<QUESTION> _list = new List<QUESTION>();
            String sql = "Select * From dbo.QUESTION_FIGURE";
            DataTable table = clsDataAccessHelper.ExecuteQueryNonStoredProcedure(sql, null);
            if (table != null && table.Rows.Count > 0)
            {
                int max = 0;
                foreach (DataRow row in table.Rows)
                {
                    object item = row["QUESTION_FIGURE_ID"];
                    if (item != null)
                    {
                        int _tmp = -1;
                        if (int.TryParse(item.ToString(), out _tmp))
                        {
                            if (_tmp > max) max = _tmp;
                        }
                    }
                }
                return max.ToString();
            }
            return "";
        }

        public static bool Delete(Object tmp)
        {
            QUESTION_FIGURE obj = (QUESTION_FIGURE)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            //_list.Add(new SqlParameter("@FIGURE_ID", obj.FIGURE_ID));
            //_list.Add(new SqlParameter("@QUESTION_FIGURE_DESC", obj.QUESTION_FIGURE_DESC));
            _list.Add(new SqlParameter("@QUESTION_FIGURE_ID", obj.QUESTION_FIGURE_ID));
            //_list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_figure_delete", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static List<QUESTION_FIGURE> LoadAll()
        {
            List<QUESTION_FIGURE> _list = new List<QUESTION_FIGURE>();
            //call stored getlist here
            return _list;
        }
    }
}
